﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace SKN.TestDataTool
{
    public class SharePointUtility
    {
        public string _message = string.Empty;
        public string _error = string.Empty;

        public void LoadDiscussionList (SPList list, int discussionCount, int maxResponsesCount, bool cleanListFirst)
        {
            for (int i = 0; i < discussionCount; i++)
            {
                string discussionSubject = string.Format("This is the subject of Discussion {0}", i);
                SPListItem objDiscussion = SPUtility.CreateNewDiscussion(list.Items, discussionSubject);

                objDiscussion[SPBuiltInFieldId.Body] = "This is the Body of the Discussion";

                objDiscussion.Update();
                _message += Environment.NewLine + string.Format("Discussion {0} added. ", i);

                Random r = new Random();
                int randomResponseCount = r.Next(0, maxResponsesCount);
                for (int j = 0; j < randomResponseCount; j++)
                {
                    SPListItem reply = SPUtility.CreateNewDiscussionReply(objDiscussion);
                    reply["Body"] = string.Format("Discussion's reply {0}", j);
                    reply.Update();

                    _message += Environment.NewLine + string.Format("Reply {0} added", j);
                }
            }
        }

        public void LoadGenericList (SPList list, int itemCount, bool cleanListFirst)
        {
            Random r = new Random();
            int randomNumber = 0;
            bool seedExist = false;

            seedExist = list.ItemCount > 0;

            SPFieldCollection fields = list.Fields;

            Dictionary<string, string> fieldsSelected = new Dictionary<string, string>();

            foreach (SPField field in fields)
            {   //Check 1 : Skip hidden and readonly  field
                if (!(field.Hidden || field.ReadOnlyField))
                {   //Check 2 : Pick only the one with particular data-type
                    if (field.Type == SPFieldType.Text
                    || field.Type == SPFieldType.Note
                    || field.Type == SPFieldType.Number
                    || field.Type == SPFieldType.Currency
                    || field.Type == SPFieldType.Boolean
                    || field.Type == SPFieldType.Choice
                    || field.Type == SPFieldType.DateTime
                    || field.Type == SPFieldType.User)
                    {
                        fieldsSelected.Add(field.Title, field.TypeAsString);
                    }
                }
            }

            // Check to find if seed exist
            SPListItem listItemSeed = null;
            if (seedExist)
            {
                listItemSeed = list.Items[0];
            }

            // This will to delete all the items from the list except the seed item
            int itemsToDelete = list.ItemCount;
            if (cleanListFirst)
            {
                for (int j = itemsToDelete - 1; j > 0; j--)
                {
                    list.Items[j].Delete();
                }
            }

            // Start creating test data
            for (int i = 0; i < itemCount; i++)
            {
                SPListItem item = list.Items.Add();

                foreach (KeyValuePair<string, string> fieldTitle in fieldsSelected)
                {
                    if (seedExist) // seed exist 
                    {
                        object seedValue;

                        // check for null 
                        if (null != listItemSeed[fieldTitle.Key])
                        {
                            seedValue = listItemSeed[fieldTitle.Key];

                            // if field is type=text , we append the counter 
                            switch (fieldTitle.Value)
                            {
                                case "Text":  // Does the same as Note 
                                case "Note":
                                    item[fieldTitle.Key] = string.Format("{0} {1}", seedValue.ToString(), i);
                                    break;
                                case "Choice":
                                    SPFieldChoice spChoiceField = list.Fields[fieldTitle.Key] as SPFieldChoice;
                                    randomNumber = r.Next(0, spChoiceField.Choices.Count - 1);
                                    item[fieldTitle.Key] = spChoiceField.Choices[randomNumber];
                                    break;
                                case "Number":
                                case "Currency":
                                    randomNumber = r.Next(0, 100);
                                    double doubleSeedValue = (double)seedValue;
                                    if (doubleSeedValue <= 1)
                                    {   
                                        // Get a new random value between 0-1
                                        item[fieldTitle.Key] = ((double)randomNumber) / 100.00d;
                                    }
                                    else
                                    {   
                                        // Get a new random value between 0-100
                                        item[fieldTitle.Key] = randomNumber;
                                    }
                                    break;
                                case "Boolean":
                                    randomNumber = r.Next(0, 100);
                                    item[fieldTitle.Key] = (randomNumber > 50);
                                    break;
                                default:
                                    item[fieldTitle.Key] = seedValue;
                                    break;
                            }
                        }
                    }
                    else // seed missing
                    {
                        item[fieldTitle.Key] = string.Format("{0} {1}", fieldTitle, i);
                    }
                }
                item.Update();
            }
            list.Update();
        }
    }
}
